package com.easydisk.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easydisk.admin.feignclient.FileFeignClient;
import com.easydisk.admin.mapper.RoleMapper;
import com.easydisk.admin.mapper.UserAndRoleMapper;
import com.easydisk.admin.mapper.UserMapper;
import com.easydisk.admin.model.req.AddUserReq;
import com.easydisk.admin.pojo.Role;
import com.easydisk.admin.pojo.User;
import com.easydisk.admin.pojo.UserAndRole;
import com.easydisk.admin.service.UserService;
import com.easydisk.constant.FileConstant;
import com.easydisk.constant.UserConstant;
import com.easydisk.model.req.AddRootFolderReq;
import com.easydisk.common.util.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserAndRoleMapper userAndRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private FileFeignClient fileFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUser(AddUserReq addUserReq) {
        // 判断用户名是否存在
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("username", addUserReq.getUsername());
        User exit = userMapper.selectOne(query);
        if (exit != null) {
            throw new RuntimeException("用户名重复,新增用户失败");
        }
        // 判断邮箱是否重复
        long emailCnt = this.count(new QueryWrapper<User>().eq("email", addUserReq.getEmail()));
        if (emailCnt != 0) {
            throw new RuntimeException("邮箱重复,新增用户失败");
        }

        // 判断手机号是否重复
        long mobileCnt = this.count(new QueryWrapper<User>().eq("mobile", addUserReq.getMobile()));
        if (mobileCnt != 0) {
            throw new RuntimeException("手机号重复,新增用户失败");
        }

        // 判断角色id是否存在
        Role role = roleMapper.selectById(addUserReq.getRoleId());
        if (role == null) {
            throw new RuntimeException("角色不存在,新增用户失败");
        }
        // 拼装用户信息
        User user = new User();
        String encode = passwordEncoder.encode(addUserReq.getPassword());
        Long userId = IdGenerator.nextId();
        user.setId(userId);
        user.setUsername(addUserReq.getUsername());
        user.setMobile(addUserReq.getMobile());
        user.setEmail(addUserReq.getEmail());
        user.setPassword(encode);
        user.setDiskMaxSize(addUserReq.getDiskMaxSize());
        user.setStatus(UserConstant.UserStatusEnum.YES.getCode());
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        //插入用户信息
        userMapper.insert(user);

        // 拼装用户角色信息
        UserAndRole userAndRole = new UserAndRole();
        userAndRole.setId(IdGenerator.nextId());
        userAndRole.setUserId(userId);
        userAndRole.setRoleId(addUserReq.getRoleId());
        // 插入用户角色信息
        userAndRoleMapper.insert(userAndRole);

        AddRootFolderReq addRootFolderReq = new AddRootFolderReq();
        addRootFolderReq.setUserId(userId);
        addRootFolderReq.setFolderName(FileConstant.ALL_FILE_CN_STR);
        addRootFolderReq.setParentId(FileConstant.ROOT_FOLDER_PARENT_ID);
        fileFeignClient.addRootFolder(addRootFolderReq);

    }
}